package com.cq.service.Impl;

import com.cq.dao.LoginDao;
import com.cq.pojo.LoginUser;
import com.cq.service.LoginService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author lap
 * @version 1.0
 * @date 2021/8/12 13:53
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginDao loginDao;

    @Override
    public LoginUser selectByUsername(String username) {
        return loginDao.selectByUsername(username);
    }
    @Override
    public LoginUser QueryLoginByStudent(Integer username,String password) {
        System.out.println("==============>"+username);
        System.out.println("==============>"+password);
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //判断当前是否已经认证过
        if(!subject.isAuthenticated()){
            //创建一个认证令牌
            UsernamePasswordToken token = new UsernamePasswordToken(String.valueOf(username),password);
            //做登录
            //做登录
            try{
                subject.login(token);
            }catch (UnknownAccountException e){
                //未知账号异常

                return null;
            }catch (IncorrectCredentialsException e){

                return null;
            }catch (AuthenticationException e){
                return null;
            }

        }
        //认证通过，获取已保存在shiro session域中的用户信息
        Session session = subject.getSession();
        LoginUser loginUser =(LoginUser) session.getAttribute("loginUsers");
        System.out.println("----------------========================>"+loginUser);
        //直接返回给控制器方法
        return loginUser;
    }

    @Override
    public LoginUser QueryLoginByEmp(String username, String password) {
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //判断当前是否已经认证过
        if(!subject.isAuthenticated()){
            //创建一个认证令牌
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
            //做登录

            //做登录
            try{
                subject.login(token);
            }catch (UnknownAccountException e){
                //未知账号异常

                return null;
            }catch (IncorrectCredentialsException e){

                return null;
            }catch (AuthenticationException e){
                return null;
            }
        }
        //认证通过，获取已保存在shiro session域中的用户信息
        Session session = subject.getSession();
        LoginUser loginUser =(LoginUser) session.getAttribute("loginUsers");
        System.out.println("----------------========================>"+loginUser);
        //直接返回给控制器方法
        return loginUser;
    }

   /* @Override
    public LoginUser QueryLoginByAdmin(String username, String password) {

        System.out.println("==============>"+username);
        System.out.println("==============>"+password);
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //判断当前是否已经认证过
        if(!subject.isAuthenticated()){
            //创建一个认证令牌
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
            //做登录
            subject.login(token);
        }
        //认证通过，获取已保存在shiro session域中的用户信息
        Session session = subject.getSession();
        LoginUser loginUser =(LoginUser) session.getAttribute("loginUsers");
        System.out.println("----------------========================>"+loginUser);
        //直接返回给控制器方法
        return loginUser;
    }
*/

    @Override
    public LoginUser selectByUsernameByEmp(String username) {
        return loginDao.selectByUsernameByEmp(username);
    }

    @Override
    public LoginUser selectByUsernameByStudent(Integer username) {
        return loginDao.selectByUsernameByStudent(username);
    }

    @Override
    public LoginUser loginOut() {
        return loginDao.loginOut();
    }
}
